package model

import (
	"server/common"
	"server/pkg/app"
)

type Performance struct {
	Model
	Name string `gorm:"column:name;type:varchar(200)" json:"name"`
	Desc string `gorm:"column:desc;type:longtext" json:"desc"`

	Fio []Fio
}

func (fio *Performance) TableName() string {
	return "starttime"
}

func (performance *Performance) List(page *app.Page) ([]Fio, int64) {
	performance_list := []Fio{}
	var count int64
	db := common.DB
	db.Order("cti desc")
	if page.Page == -1 {
		if err := db.Find(&performance_list).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
			return performance_list, count
		}
	} else {
		if err := db.Limit(page.Size).Offset((page.Page - 1) * page.Size).Find(&performance_list).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
			return performance_list, count
		}
	}
	return performance_list, count
}

func (performance *Performance) Info() error {
	if err := common.DB.Where("id = ?", performance.ID).Find(&performance).Error; err != nil {
		return err
	}
	return nil
}

func (performance *Performance) Create() error {
	if err := common.DB.Create(&performance).Error; err != nil {
		return err
	}
	return nil
}

func (performance *Performance) Update() error {
	if err := common.DB.Where("id = ?", performance.ID).Updates(&performance).Error; err != nil {
		return err
	}
	return nil
}

func (performance *Performance) Delete() error {
	if err := common.DB.Where("id = ?", performance.ID).Delete(&performance).Error; err != nil {
		return err
	}
	return nil
}
